const mongoose = require('mongoose');
require('dotenv').config();
const Hotel = require('./models/Hotel');

// 连接数据库
const connectDB = async () => {
  try {
    await mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/traveller_social_app', {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    });
    console.log('✅ MongoDB 连接成功');
  } catch (error) {
    console.error('❌ MongoDB 连接失败:', error);
    process.exit(1);
  }
};

// 酒店数据
const hotelsData = [
  {
    name: '成都香格里拉大酒店',
    description: '位于成都市中心的豪华五星级酒店，享有城市美景，提供优质服务和现代化设施。',
    address: '成都市锦江区滨江东路9号',
    city: '成都',
    country: '中国',
    coordinates: {
      latitude: 30.6586,
      longitude: 104.0647
    },
    starRating: 5,
    price: 1200,
    originalPrice: 1500,
    images: [
      'https://images.unsplash.com/photo-1566073771259-6a8506099945?w=800',
      'https://images.unsplash.com/photo-1582719478250-c89cae4dc85b?w=800',
      'https://images.unsplash.com/photo-1571896349842-33c89424de2d?w=800'
    ],
    mainImage: 'https://images.unsplash.com/photo-1566073771259-6a8506099945?w=800',
    amenities: ['免费WiFi', '健身房', '游泳池', '商务中心', 'SPA', '餐厅', '酒吧', '停车场'],
    roomTypes: [
      {
        name: '豪华客房',
        price: 1200,
        description: '40平米豪华客房，配备现代化设施',
        maxGuests: 2,
        amenities: ['免费WiFi', '迷你吧', '保险箱', '浴袍']
      },
      {
        name: '行政套房',
        price: 2000,
        description: '80平米行政套房，享有城市景观',
        maxGuests: 4,
        amenities: ['免费WiFi', '迷你吧', '保险箱', '浴袍', '客厅', '行政酒廊']
      }
    ],
    rating: 4.8,
    reviewCount: 2156,
    isAvailable: true,
    isPopular: true,
    checkInTime: '15:00',
    checkOutTime: '12:00',
    policies: ['免费取消', '不含早餐', '允许携带宠物'],
    contact: {
      phone: '+86-28-8888-9999',
      email: 'chengdu@shangri-la.com',
      website: 'https://www.shangri-la.com'
    },
    tags: ['豪华', '商务', '市中心', '五星级'],
    distance: {
      airport: '距双流机场 25公里',
      city_center: '市中心',
      attractions: [
        { name: '宽窄巷子', distance: '2公里' },
        { name: '锦里古街', distance: '3公里' },
        { name: '杜甫草堂', distance: '4公里' }
      ]
    },
    discount: '立减300',
    bookingCount: 1580
  },
  {
    name: '丽江古城悦榕庄',
    description: '坐落于丽江古城核心区域的精品度假酒店，融合纳西文化与现代奢华。',
    address: '云南省丽江市古城区束河古镇',
    city: '丽江',
    country: '中国',
    coordinates: {
      latitude: 26.8721,
      longitude: 100.2240
    },
    starRating: 5,
    price: 1800,
    originalPrice: 2200,
    images: [
      'https://images.unsplash.com/photo-1520250497591-112f2f40a3f4?w=800',
      'https://images.unsplash.com/photo-1551882547-ff40c63fe5fa?w=800',
      'https://images.unsplash.com/photo-1578662996442-48f60103fc96?w=800'
    ],
    mainImage: 'https://images.unsplash.com/photo-1520250497591-112f2f40a3f4?w=800',
    amenities: ['免费WiFi', 'SPA', '温泉', '餐厅', '酒吧', '花园', '图书馆', '停车场'],
    roomTypes: [
      {
        name: '庭院别墅',
        price: 1800,
        description: '独栋别墅配私人庭院，享受宁静时光',
        maxGuests: 2,
        amenities: ['免费WiFi', '私人庭院', '壁炉', '浴缸', '迷你吧']
      },
      {
        name: '池畔别墅',
        price: 2800,
        description: '配备私人泳池的豪华别墅',
        maxGuests: 4,
        amenities: ['免费WiFi', '私人泳池', '庭院', '壁炉', '浴缸', '迷你吧']
      }
    ],
    rating: 4.9,
    reviewCount: 1543,
    isAvailable: true,
    isPopular: true,
    checkInTime: '14:00',
    checkOutTime: '12:00',
    policies: ['提前3天免费取消', '含早餐', '禁烟'],
    contact: {
      phone: '+86-888-533-1111',
      email: 'lijiang@banyanTree.com',
      website: 'https://www.banyantree.com'
    },
    tags: ['度假', '古城', '文化', '奢华'],
    distance: {
      airport: '距丽江机场 35公里',
      city_center: '古城中心',
      attractions: [
        { name: '丽江古城', distance: '步行5分钟' },
        { name: '玉龙雪山', distance: '30公里' },
        { name: '泸沽湖', distance: '200公里' }
      ]
    },
    discount: '蜜月特惠',
    bookingCount: 987
  },
  {
    name: '三亚亚龙湾瑞吉度假酒店',
    description: '坐拥亚龙湾绝美海景的奢华度假酒店，提供世界级的服务和设施。',
    address: '海南省三亚市亚龙湾国家旅游度假区',
    city: '三亚',
    country: '中国',
    coordinates: {
      latitude: 18.2317,
      longitude: 109.6670
    },
    starRating: 5,
    price: 2500,
    originalPrice: 3200,
    images: [
      'https://images.unsplash.com/photo-1571003123894-1f0594d2b5d9?w=800',
      'https://images.unsplash.com/photo-1584132967334-10e028bd69f7?w=800',
      'https://images.unsplash.com/photo-1582719508461-905c673771fd?w=800'
    ],
    mainImage: 'https://images.unsplash.com/photo-1571003123894-1f0594d2b5d9?w=800',
    amenities: ['免费WiFi', '私人海滩', '无边泳池', 'SPA', '健身房', '儿童俱乐部', '餐厅', '酒吧'],
    roomTypes: [
      {
        name: '海景客房',
        price: 2500,
        description: '50平米海景客房，享受无敌海景',
        maxGuests: 2,
        amenities: ['免费WiFi', '海景阳台', '迷你吧', '浴缸', '咖啡机']
      },
      {
        name: '总统套房',
        price: 8000,
        description: '200平米总统套房，极致奢华体验',
        maxGuests: 6,
        amenities: ['免费WiFi', '海景阳台', '私人泳池', '管家服务', '客厅', '餐厅']
      }
    ],
    rating: 4.7,
    reviewCount: 3421,
    isAvailable: true,
    isPopular: true,
    checkInTime: '15:00',
    checkOutTime: '11:00',
    policies: ['免费取消', '含早餐', '儿童友好'],
    contact: {
      phone: '+86-898-8855-6666',
      email: 'sanya@stregis.com',
      website: 'https://www.marriott.com'
    },
    tags: ['海滨', '度假', '奢华', '亲子'],
    distance: {
      airport: '距凤凰机场 20公里',
      city_center: '距三亚市区 25公里',
      attractions: [
        { name: '亚龙湾海滩', distance: '步行2分钟' },
        { name: '天涯海角', distance: '35公里' },
        { name: '南山寺', distance: '40公里' }
      ]
    },
    discount: '早鸟优惠',
    bookingCount: 2341
  },
  {
    name: '西安威斯汀大酒店',
    description: '位于西安市中心的现代化商务酒店，临近钟楼和古城墙，交通便利。',
    address: '陕西省西安市莲湖区大庆路6号',
    city: '西安',
    country: '中国',
    coordinates: {
      latitude: 34.2658,
      longitude: 108.9540
    },
    starRating: 5,
    price: 800,
    originalPrice: 1000,
    images: [
      'https://images.unsplash.com/photo-1551882547-ff40c63fe5fa?w=800',
      'https://images.unsplash.com/photo-1578662996442-48f60103fc96?w=800',
      'https://images.unsplash.com/photo-1566073771259-6a8506099945?w=800'
    ],
    mainImage: 'https://images.unsplash.com/photo-1551882547-ff40c63fe5fa?w=800',
    amenities: ['免费WiFi', '健身房', '商务中心', '餐厅', '酒吧', '会议室', '停车场'],
    roomTypes: [
      {
        name: '商务客房',
        price: 800,
        description: '35平米商务客房，现代化设施齐全',
        maxGuests: 2,
        amenities: ['免费WiFi', '办公桌', '迷你吧', '保险箱']
      },
      {
        name: '行政套房',
        price: 1500,
        description: '70平米行政套房，享有城市景观',
        maxGuests: 3,
        amenities: ['免费WiFi', '客厅', '办公桌', '迷你吧', '行政酒廊']
      }
    ],
    rating: 4.5,
    reviewCount: 1876,
    isAvailable: true,
    isPopular: false,
    checkInTime: '14:00',
    checkOutTime: '12:00',
    policies: ['免费取消', '不含早餐', '商务友好'],
    contact: {
      phone: '+86-29-8738-8888',
      email: 'xian@westin.com',
      website: 'https://www.marriott.com'
    },
    tags: ['商务', '市中心', '历史', '文化'],
    distance: {
      airport: '距咸阳机场 45公里',
      city_center: '市中心',
      attractions: [
        { name: '钟楼', distance: '1公里' },
        { name: '兵马俑', distance: '45公里' },
        { name: '大雁塔', distance: '8公里' }
      ]
    },
    discount: '商务优惠',
    bookingCount: 1234
  },
  {
    name: '桂林漓江大瀑布饭店',
    description: '桂林市区唯一的瀑布主题酒店，享有漓江美景，独特的自然景观。',
    address: '广西桂林市象山区滨江路1号',
    city: '桂林',
    country: '中国',
    coordinates: {
      latitude: 25.2736,
      longitude: 110.2900
    },
    starRating: 4,
    price: 650,
    originalPrice: 800,
    images: [
      'https://images.unsplash.com/photo-1520250497591-112f2f40a3f4?w=800',
      'https://images.unsplash.com/photo-1582719478250-c89cae4dc85b?w=800',
      'https://images.unsplash.com/photo-1571896349842-33c89424de2d?w=800'
    ],
    mainImage: 'https://images.unsplash.com/photo-1520250497591-112f2f40a3f4?w=800',
    amenities: ['免费WiFi', '人工瀑布', '江景餐厅', 'SPA', '健身房', '商务中心', '停车场'],
    roomTypes: [
      {
        name: '江景客房',
        price: 650,
        description: '30平米江景客房，欣赏漓江美景',
        maxGuests: 2,
        amenities: ['免费WiFi', '江景阳台', '迷你吧', '茶具']
      },
      {
        name: '瀑布套房',
        price: 1200,
        description: '60平米瀑布景观套房，独特体验',
        maxGuests: 3,
        amenities: ['免费WiFi', '瀑布景观', '客厅', '迷你吧', '茶具']
      }
    ],
    rating: 4.3,
    reviewCount: 1456,
    isAvailable: true,
    isPopular: false,
    checkInTime: '14:00',
    checkOutTime: '12:00',
    policies: ['免费取消', '含早餐', '景观房'],
    contact: {
      phone: '+86-773-282-2881',
      email: 'guilin@waterfallhotel.com',
      website: 'https://www.guilinwaterfall.com'
    },
    tags: ['江景', '瀑布', '自然', '特色'],
    distance: {
      airport: '距两江机场 30公里',
      city_center: '市中心',
      attractions: [
        { name: '象鼻山', distance: '2公里' },
        { name: '两江四湖', distance: '1公里' },
        { name: '阳朔', distance: '65公里' }
      ]
    },
    discount: '江景特惠',
    bookingCount: 876
  },
  {
    name: '青岛海泉湾维景国际大酒店',
    description: '位于青岛海滨的度假酒店，享有黄海美景，提供温泉和海滨休闲体验。',
    address: '山东省青岛市黄岛区滨海大道1777号',
    city: '青岛',
    country: '中国',
    coordinates: {
      latitude: 36.0671,
      longitude: 120.3826
    },
    starRating: 4,
    price: 580,
    originalPrice: 720,
    images: [
      'https://images.unsplash.com/photo-1571003123894-1f0594d2b5d9?w=800',
      'https://images.unsplash.com/photo-1584132967334-10e028bd69f7?w=800',
      'https://images.unsplash.com/photo-1578662996442-48f60103fc96?w=800'
    ],
    mainImage: 'https://images.unsplash.com/photo-1571003123894-1f0594d2b5d9?w=800',
    amenities: ['免费WiFi', '温泉', '海滨浴场', '游泳池', '健身房', '餐厅', '酒吧', '儿童乐园'],
    roomTypes: [
      {
        name: '海景客房',
        price: 580,
        description: '32平米海景客房，面朝大海',
        maxGuests: 2,
        amenities: ['免费WiFi', '海景阳台', '迷你吧', '浴缸']
      },
      {
        name: '家庭套房',
        price: 980,
        description: '65平米家庭套房，适合亲子出行',
        maxGuests: 4,
        amenities: ['免费WiFi', '海景阳台', '客厅', '儿童用品', '迷你吧']
      }
    ],
    rating: 4.4,
    reviewCount: 2187,
    isAvailable: true,
    isPopular: false,
    checkInTime: '15:00',
    checkOutTime: '11:00',
    policies: ['免费取消', '含早餐', '亲子友好'],
    contact: {
      phone: '+86-532-8668-8888',
      email: 'qingdao@wjhotel.com',
      website: 'https://www.wjhotel.com'
    },
    tags: ['海滨', '温泉', '亲子', '度假'],
    distance: {
      airport: '距胶东机场 35公里',
      city_center: '距市区 25公里',
      attractions: [
        { name: '金沙滩', distance: '步行5分钟' },
        { name: '青岛啤酒博物馆', distance: '30公里' },
        { name: '栈桥', distance: '35公里' }
      ]
    },
    discount: '温泉套餐',
    bookingCount: 1654
  }
];

// 初始化酒店数据
const initializeHotelData = async () => {
  try {
    console.log('🏨 开始初始化酒店数据...\n');

    // 清空现有数据
    await Hotel.deleteMany({});
    console.log('✅ 清空现有酒店数据');

    // 插入新数据
    const hotels = await Hotel.insertMany(hotelsData);
    console.log(`✅ 成功创建 ${hotels.length} 个酒店`);

    // 打印统计信息
    console.log('\n📊 酒店数据统计:');
    console.log(`   🏨 总计酒店: ${hotels.length} 个`);
    
    const cities = [...new Set(hotels.map(h => h.city))];
    console.log(`   🌍 覆盖城市: ${cities.join(', ')}`);
    
    const starRatings = hotels.reduce((acc, h) => {
      acc[h.starRating] = (acc[h.starRating] || 0) + 1;
      return acc;
    }, {});
    console.log('   ⭐ 星级分布:');
    Object.entries(starRatings).forEach(([star, count]) => {
      console.log(`      ${star}星级: ${count} 个`);
    });

    const priceRange = {
      min: Math.min(...hotels.map(h => h.price)),
      max: Math.max(...hotels.map(h => h.price)),
      avg: Math.round(hotels.reduce((sum, h) => sum + h.price, 0) / hotels.length)
    };
    console.log(`   💰 价格范围: ¥${priceRange.min} - ¥${priceRange.max} (平均: ¥${priceRange.avg})`);

    console.log('\n🎉 酒店数据初始化完成！');
    return hotels;

  } catch (error) {
    console.error('❌ 酒店数据初始化失败:', error);
    throw error;
  }
};

// 运行初始化
const runInit = async () => {
  try {
    await connectDB();
    await initializeHotelData();
    
    console.log('\n✅ 所有数据初始化完成！');
    console.log('🚀 现在可以启动应用程序了');
    
  } catch (error) {
    console.error('❌ 初始化过程中发生错误:', error);
  } finally {
    await mongoose.connection.close();
    console.log('📝 数据库连接已关闭');
  }
};

// 如果直接运行此文件
if (require.main === module) {
  runInit();
}

module.exports = { initializeHotelData }; 